package org.example.pcbcircuitboard.controller;

import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.example.pcbcircuitboard.constants.StatusCode;
import org.example.pcbcircuitboard.constants.StatusData;
import org.example.pcbcircuitboard.pojo.PowerSupplyPojo;
import org.example.pcbcircuitboard.service.PowerSupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 电源命令接口
 */
@RestController
@RequestMapping("/mqtt-power-supply")
public class MqttPowerSupplyController {
    // 连接客户端
    @Autowired
    private IMqttClient mqttClient;
    @Autowired
    private PowerSupplyService powerSupplyService;

    /**
     * 电源开关 启动 停止
     * @param powerSupplyPojo 电源信息
     * @return 状态数据 0成功 1失败
     */
    @PostMapping("/turn-on")
    public StatusData<Integer,String> turnOn(@RequestBody  PowerSupplyPojo powerSupplyPojo) {
        boolean result = powerSupplyService.encapsulationMqttData(powerSupplyPojo, mqttClient);
        if (result){
            return new StatusData<>(StatusCode.SUCCESS, "Turn on power supply successfully");
        }else {
            return new StatusData<>(StatusCode.ERROR, "Failed to turn on power supply");
        }
    }
}
